package service;

import beans.Vehicle;
import db.VehicleDao;
import java.sql.SQLException;
import java.util.List;
import java.sql.Date;
public class VehicleService {
    private VehicleDao vehicleDao;

    public VehicleService() {
        this.vehicleDao = new VehicleDao();
    }

    //搜索
    public List<Vehicle> searchVehicles(String vehicleIdStr, String licensePlate,
                                        String brandModel, String color, String purchaseDateStr,String status) {

        // 参数转换
        Integer vehicleId = null;
        try {
            if (vehicleIdStr != null && !vehicleIdStr.isEmpty()) {
                vehicleId = Integer.parseInt(vehicleIdStr);
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("车辆ID必须为数字");
        }

        Date purchaseDate = null;
        try {
            if (purchaseDateStr != null && !purchaseDateStr.isEmpty()) {
                purchaseDate = java.sql.Date.valueOf(purchaseDateStr);
            }
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("日期格式错误，应为yyyy-MM-dd");
        }
        if (status != null && !status.isEmpty()) {
            if (!"空闲".equals(status) && !"使用".equals(status)) {
                throw new IllegalArgumentException("状态只能是'空闲'或'使用'");
            }
        }

        return vehicleDao.searchVehicles(
                vehicleId,
                licensePlate,
                brandModel,
                color,
                purchaseDate,
                status
        );
    }

    // 添加车辆
    public boolean addVehicle(Vehicle vehicle) {
        try {
            return vehicleDao.insertVehicle(vehicle);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 获取所有车辆
    public List<Vehicle> getAllVehicles() {
        try {
            return vehicleDao.getAllVehicles();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取所有可用车辆
    public List<Vehicle> getAllAvailableVehicles() {
        try {
            return vehicleDao.getAvailableVehicles();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 更新车辆信息
    public boolean updateVehicle(Vehicle vehicle) {
        try {
            return vehicleDao.updateVehicle(vehicle);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除车辆
    public boolean deleteVehicle(int vehicleId) {
        try {
            return vehicleDao.deleteVehicle(vehicleId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 获取所有车辆ID
    public List<Integer> getAllVehicleIds() {
        try {
            return vehicleDao.getAllVehicleIds();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Object searchVehicles(String keyword) throws SQLException {
        VehicleDao userDao=new VehicleDao();
        return userDao.getAllVehicles();
    }

    public Vehicle getVehicleById(int vehicleId) {
        try {
            return vehicleDao.getVehicleById(vehicleId);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}